package com.syh.jn.scm.service.impl.downstream.finance;

import com.alibaba.dubbo.config.annotation.Service;
import com.syh.core.ddd.infrastructure.repository.AggregateRepository;
import com.syh.core.ddd.service.AbstractServiceImpl;
import com.syh.jn.scm.domain.downstream.entity.finance.DownPaymentBill;
import com.syh.jn.scm.domain.downstream.event.DownPaymentBillEvent;
import com.syh.jn.scm.domain.downstream.repository.finance.DownPaymentBillRepository;
import com.syh.jn.scm.dto.downstream.finance.DownPaymentBillMto;
import com.syh.jn.scm.dto.downstream.finance.DownPaymentBillSto;
import com.syh.jn.scm.dto.downstream.finance.DownPaymentBillVo;
import com.syh.jn.scm.service.api.downstream.finance.DownPaymentBillService;
import com.syh.jn.scm.service.api.downstream.finance.enums.DownPaymentBillEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * Date:2018/7/19
 * Author:Clown
 * 下游收款单接口实现;
 */
@Service
@Transactional
public class DownPaymentBillServiceImpl extends AbstractServiceImpl<DownPaymentBillSto, DownPaymentBillMto, DownPaymentBillVo, DownPaymentBill, Long> implements DownPaymentBillService {
  @Autowired
  private DownPaymentBillRepository downstreamPaymentBillRepositoryImpl;

  @Override
  protected AggregateRepository<DownPaymentBill, Long> getAggregateRepository() {
    return downstreamPaymentBillRepositoryImpl;
  }

  /**
   * 新增下游付款单
   *
   * @param sto
   * @return
   */
  @Override
  public Long save(DownPaymentBillSto sto) {
    DownPaymentBill bill = new DownPaymentBill(sto);
    Long id = downstreamPaymentBillRepositoryImpl.add(bill);
    this.publicEvent(DownPaymentBillEvent.receiptBillCreated(new Object[]{bill}));
    return id;
  }

  /**
   * 修改下游付款单
   *
   * @param mto
   */
  @Override
  public void update(DownPaymentBillMto mto) {
    DownPaymentBill bill = downstreamPaymentBillRepositoryImpl.getById(mto.getId());
    bill.modify(mto);
    downstreamPaymentBillRepositoryImpl.update(bill);
  }

  /**
   * 删除下游付款单
   *
   * @param id
   */
  @Override
  public void delete(Long id) {
    DownPaymentBill bill = downstreamPaymentBillRepositoryImpl.getById(id);
    bill.destory();
    downstreamPaymentBillRepositoryImpl.remove(bill);
    this.publicEvent(DownPaymentBillEvent.receiptBillDestory(new Object[]{bill}));
  }

  /**
   * 下游付款单确认付款
   *
   * @param id
   */
  @Override
  public void confirm(Long id) {
    DownPaymentBill bill = downstreamPaymentBillRepositoryImpl.getById(id);
    bill.confirm();
    downstreamPaymentBillRepositoryImpl.update(bill);

    //设置对账单的到账金额(事件的方式实现)
    //发送事件消息（只有双方都确认的情况下）
    if (bill.getLockStatus().equals(DownPaymentBillEnum.LOCK.getStatus())) {
      this.publicEvent(DownPaymentBillEvent.receiptBillLock(new Object[]{bill}));
    }
  }

  /**
   * 下游取消付款
   *
   * @param id
   */
  @Override
  public void cancel(Long id) {
    DownPaymentBill bill = downstreamPaymentBillRepositoryImpl.getById(id);
    bill.cancel();
    downstreamPaymentBillRepositoryImpl.update(bill);

    //设置对账单的到账金额(事件的方式实现),收款状态
    //发送事件消息（有一方取消情况下）(不锁定状态并且是已到账)
    if (bill.getLockStatus().equals(DownPaymentBillEnum.UNLOCK.getStatus()) && bill.getReceiptState().equals(DownPaymentBillEnum.OUR_CONFIRM.getStatus())) {
      this.publicEvent(DownPaymentBillEvent.receiptBillUnlock(new Object[]{bill}));
    }
  }
}
